package com.autoplaza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Buscar extends Activity {
	String result, marcalista=null,modelolista=null, anolista=null, ubicacionlista=null,preciolista=null;
	InputStream is = null;
	String[] arr2,array2, array3;
	Button marca, ano, ubicacion, precio, modelo, buscar, limpiar, btnmenu;
	AlertDialog.Builder builder;
	AlertDialog alertDialog;
	String modeloconcate, marcaconcate,anoconcate, ubicaconcate;
	int var=0,var2;
	StringBuilder sb1, sb2, sb3, sb4, sb5;
	private String textoEnviar2,textoEnviar3;
	private String cadena2,cadena3,cadena4;
	String contenido;
	EditText content ;
	private Object cadenabase;
	static boolean bTieneConexion;
	ConnectivityManager cm;
	boolean metodo;
	WebView myWebView;
	//	ProgressBar progress;
	int slide;

	//Método Pause
	@Override
	protected void onPause(){
		SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		super.onPause();
	}

	//Método OnBack
	public void onBackPressed() {
		finish();
	}

	//Método OnResume
	@Override
	protected void onResume() {

		SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
		marca.setText(preferences.getString("marca", "[Cualquiera]"));
		modelo.setText(preferences.getString("modelo", "[Cualquiera]"));
		ano.setText(preferences.getString("ano", "[Cualquiera]"));
		ubicacion.setText(preferences.getString("ubicacion", "[Cualquiera]"));
		precio.setText(preferences.getString("precio", "[Cualquiera]"));
		//			content.setText(preferences.getString("content", "Ejemplo : Jetta Rojo 2005"));
		Log.d("Buscar",preferences.getString("modelo", "no hay nada"));
		Log.d("BuscarMarca",preferences.getString("marca", "no hay nada"));
		metodo = isOnline();
		if(metodo==false){
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.necesita_conexi_n_a_internet)
			.setCancelable(false)
			.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					finish();
				}
			});
			AlertDialog alertDialog = builder.create();
			alertDialog.show();

		}else{
			myWebView.setBackgroundColor(Color.parseColor("#00000000"));
			myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

			myWebView.loadUrl("http://iphone.autoplaza.com.mx/BannersAndroid/Banners.aspx?home=yes&");
		}
		super.onResume();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}


	//	@Override
	//	public void onWindowFocusChanged(boolean hasFocus) {
	//		// TODO Auto-generated method stub
	//		super.onWindowFocusChanged(hasFocus);
	//		
	//		LinearLayout rl_buscador = (LinearLayout) findViewById(R.id.relative_buscador);
	//		RelativeLayout rl_btn = (RelativeLayout) findViewById(R.id.relativebtns);
	//		Log.i("tam relative", ""+rl_buscador.getWidth());
	//		
	//		int[] dimens = Utils.getScreenSize(Buscar.this);
	//
	//		int espacio = dimens[0]-rl_buscador.getWidth();
	//		int espacioentredos = (int)(espacio/2);
	//		Log.i("espacio", ""+espacio);
	//		Log.i("espacio/2", ""+espacioentredos);
	//		Log.i("btnmenu", ""+btnmenu.getWidth());
	//
	//		slide = espacioentredos+btnmenu.getWidth();
	//		Log.i("slide", ""+slide);
	//
	////		inicio, contacto, preguntas frecuentes, sugerencias y links a redes sociales
	//		
	//		btnmenu.setOnClickListener(new OnClickListener() {
	//			
	//			@Override
	//			public void onClick(View v) {
	//				int[] dimen = Utils.getScreenSize(Buscar.this);
	//				int width = 0; 
	//				
	//	    		if(dimen[0] <= 320){
	//	            	Log.i("DENSITY", "MEDIUM");
	//	            	width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, btnmenu.getWidth()+30, getResources().getDisplayMetrics());
	//	        	}else if( (dimen[0] > 320) && (dimen[0] <=480 )){
	//	            	Log.i("DENSITY", "HIGH");
	//	            	width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, btnmenu.getWidth()+45, getResources().getDisplayMetrics());
	//	        	}else if(dimen[0] > 480){
	//	            	Log.i("DENSITY", "EXTRA HIGH");
	//	            	width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, btnmenu.getWidth()+45, getResources().getDisplayMetrics());
	//	        	}
	//				
	//				SlideoutActivity.prepare(Buscar.this, R.id.inner_content, width);
	//				startActivity(new Intent(Buscar.this,
	//						MenuActivity.class));
	//				overridePendingTransition(0, 0);				
	//			}
	//		});
	//		
	//		LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
	//		lp2.setMargins(espacioentredos, 40, espacioentredos, 20);
	//		rl_btn.setLayoutParams(lp2);
	//		
	//		int size = Utils.getScreenSize2(Buscar.this);
	//		if(size == 1){
	//			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 20);
	//			lp.setMargins(espacioentredos, 2, 0, 2);
	//			btnmenu.setLayoutParams(lp);
	//		}
	//		if(size == 2){
	//			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(45, 45);
	//			lp.setMargins(espacioentredos, 2, 0, 2);
	//			btnmenu.setLayoutParams(lp);
	//		}
	//		if(size == 3){
	//			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(60, 60);
	//			lp.setMargins(espacioentredos, 2, 0, 2);
	//			btnmenu.setLayoutParams(lp);
	//		}
	//		
	//		
	//
	//	}

	@Override
	protected void onDestroy() {
		SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean("firsttime", true).commit();
		super.onDestroy();
		Log.i("destroy", "destroy");
		//		Toast.makeText(getApplicationContext(), "destroy", Toast.LENGTH_SHORT).show();
	}

	//Método OnCreate
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.buscarnuevo);


		Log.i("oncreate", "oncreate");

		//		progress = (ProgressBar) findViewById(R.id.progress_wait);


		myWebView = (WebView)this.findViewById(R.id.web);
		myWebView.getSettings().setJavaScriptEnabled(true);
		//        myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
		//		myWebView.
		//		myWebView.setBackgroundColor(Color.BLUE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		//Declarar preferencias
		SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();

		//		metodo = isOnline();
		//		if(metodo==false){
		//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
		//				builder.setMessage("Necesita conexión a internet")
		//				.setCancelable(false)
		//				.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
		//					public void onClick(DialogInterface dialog, int id) {
		//							finish();
		//						}
		//				});
		//				AlertDialog alertDialog = builder.create();
		//				alertDialog.show();
		//			
		//		}else{
		//	        myWebView.loadUrl("http://iphone.autoplaza.com.mx/BannersAndroid/Banners.aspx?home=yes&");
		//			
		//		}


		//Declarar botones
		modelo = (Button) findViewById(R.id.modelo);
		marca = (Button) findViewById(R.id.marca);
		ano = (Button) findViewById(R.id.ano);
		ubicacion = (Button) findViewById(R.id.ubicacion);
		precio= (Button) findViewById(R.id.precio);
		content = (EditText) findViewById(R.id.content);
		content.setInputType(InputType.TYPE_CLASS_TEXT);
		btnmenu = (Button) findViewById(R.id.btn_menu);
		btnmenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent goManuLateral = new Intent(Buscar.this, MenuLateral.class);
				goManuLateral.putExtra("clase","Buscar");
				startActivity(goManuLateral);
			}
		});

		// Get data about the device's display
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		float densityPxPerDp = displayMetrics.density;
		float densityPxPerSp = displayMetrics.scaledDensity;
		float densityBucketDpi = displayMetrics.densityDpi;
		float xdpi = displayMetrics.xdpi;
		float ydpi = displayMetrics.ydpi;

		if(densityPxPerSp > 2){
			LinearLayout.LayoutParams para = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);	
			para.setMargins(0, 40, 0, 0);
			para.gravity = Gravity.CENTER_HORIZONTAL;
			TextView texto = (TextView) findViewById(R.id.textView3);
			TextView texto1 = (TextView) findViewById(R.id.textView4);
			TextView texto2 = (TextView) findViewById(R.id.textView5);
			TextView texto3 = (TextView) findViewById(R.id.textView6);
			TextView texto4 = (TextView) findViewById(R.id.textView7);

			//						texto.setGravity(Gravity.CENTER);
			texto.setLayoutParams(para);
			texto1.setLayoutParams(para);
			texto2.setLayoutParams(para);
			texto3.setLayoutParams(para);
			texto4.setLayoutParams(para);



			RelativeLayout lin = (RelativeLayout) findViewById(R.id.linearLayout1);
			lin.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
			RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(90, 90);
			params0.addRule(RelativeLayout.CENTER_HORIZONTAL);
			params0.addRule(RelativeLayout.CENTER_VERTICAL);
			params0.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			btnmenu.setLayoutParams(params0);
			LinearLayout im = (LinearLayout) findViewById(R.id.img_titulo);
			LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(250, 482);
			params1.setMargins(0, 10, 0, 0);
			im.setLayoutParams(params1);
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);
			params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			marca.setLayoutParams(params);
			params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);
			params.addRule(RelativeLayout.BELOW, R.id.marca);
			modelo.setLayoutParams(params);
			params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);
			params.addRule(RelativeLayout.BELOW, R.id.modelo);
			ano.setLayoutParams(params);
			params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);
			params.addRule(RelativeLayout.BELOW, R.id.ano);
			ubicacion.setLayoutParams(params);
			params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100);
			params.addRule(RelativeLayout.BELOW, R.id.ubicacion);
			precio.setLayoutParams(params);
		}else{
			int size = Utils.getScreenSize2(Buscar.this);
			if( size == 1){
				LinearLayout.LayoutParams para = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);	
				para.setMargins(0, 15, 0, 0);
				para.gravity = Gravity.CENTER_HORIZONTAL;
				TextView texto = (TextView) findViewById(R.id.textView3);
				TextView texto1 = (TextView) findViewById(R.id.textView4);
				TextView texto2 = (TextView) findViewById(R.id.textView5);
				TextView texto3 = (TextView) findViewById(R.id.textView6);
				TextView texto4 = (TextView) findViewById(R.id.textView7);

				//						texto.setGravity(Gravity.CENTER);
				texto.setLayoutParams(para);
				texto1.setLayoutParams(para);
				texto2.setLayoutParams(para);
				texto3.setLayoutParams(para);
				texto4.setLayoutParams(para);

				RelativeLayout lin = (RelativeLayout) findViewById(R.id.linearLayout1);
				lin.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 30));
				RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(25, 25);
				params0.addRule(RelativeLayout.CENTER_HORIZONTAL);
				params0.addRule(RelativeLayout.CENTER_VERTICAL);
				params0.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				btnmenu.setLayoutParams(params0);
				LinearLayout im = (LinearLayout) findViewById(R.id.img_titulo);
				LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(100, 193);
				params1.setMargins(0, 5, 0, 0);
				im.setLayoutParams(params1);
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
				params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
				marca.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
				params.addRule(RelativeLayout.BELOW, R.id.marca);
				modelo.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
				params.addRule(RelativeLayout.BELOW, R.id.modelo);
				ano.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
				params.addRule(RelativeLayout.BELOW, R.id.ano);
				ubicacion.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
				params.addRule(RelativeLayout.BELOW, R.id.ubicacion);
				precio.setLayoutParams(params);
			}		
			if( size == 2){
				LinearLayout.LayoutParams para = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);	
				para.setMargins(0, 23, 0, 0);
				para.gravity = Gravity.CENTER_HORIZONTAL;
				TextView texto = (TextView) findViewById(R.id.textView3);
				TextView texto1 = (TextView) findViewById(R.id.textView4);
				TextView texto2 = (TextView) findViewById(R.id.textView5);
				TextView texto3 = (TextView) findViewById(R.id.textView6);
				TextView texto4 = (TextView) findViewById(R.id.textView7);

				//						texto.setGravity(Gravity.CENTER);
				texto.setLayoutParams(para);
				texto1.setLayoutParams(para);
				texto2.setLayoutParams(para);
				texto3.setLayoutParams(para);
				texto4.setLayoutParams(para);

				RelativeLayout lin = (RelativeLayout) findViewById(R.id.linearLayout1);
				lin.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60));	

				RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(50, 50);
				params0.addRule(RelativeLayout.CENTER_HORIZONTAL);
				params0.addRule(RelativeLayout.CENTER_VERTICAL);
				params0.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				btnmenu.setLayoutParams(params0);
				LinearLayout im = (LinearLayout) findViewById(R.id.img_titulo);
				LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(150, 289);
				params1.setMargins(0, 10, 0, 0);
				im.setLayoutParams(params1);
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);
				params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
				marca.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);
				params.addRule(RelativeLayout.BELOW, R.id.marca);
				modelo.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);
				params.addRule(RelativeLayout.BELOW, R.id.modelo);
				ano.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);
				params.addRule(RelativeLayout.BELOW, R.id.ano);
				ubicacion.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);
				params.addRule(RelativeLayout.BELOW, R.id.ubicacion);
				precio.setLayoutParams(params);

			}		
			if( size == 3){
				LinearLayout.LayoutParams para = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);	
				para.setMargins(0, 30, 0, 0);
				para.gravity = Gravity.CENTER_HORIZONTAL;
				TextView texto = (TextView) findViewById(R.id.textView3);
				TextView texto1 = (TextView) findViewById(R.id.textView4);
				TextView texto2 = (TextView) findViewById(R.id.textView5);
				TextView texto3 = (TextView) findViewById(R.id.textView6);
				TextView texto4 = (TextView) findViewById(R.id.textView7);

				//						texto.setGravity(Gravity.CENTER);
				texto.setLayoutParams(para);
				texto1.setLayoutParams(para);
				texto2.setLayoutParams(para);
				texto3.setLayoutParams(para);
				texto4.setLayoutParams(para);



				RelativeLayout lin = (RelativeLayout) findViewById(R.id.linearLayout1);
				lin.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80));
				RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(70, 70);
				params0.addRule(RelativeLayout.CENTER_HORIZONTAL);
				params0.addRule(RelativeLayout.CENTER_VERTICAL);
				params0.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				btnmenu.setLayoutParams(params0);
				LinearLayout im = (LinearLayout) findViewById(R.id.img_titulo);
				LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(200, 385);
				params1.setMargins(0, 10, 0, 0);
				im.setLayoutParams(params1);
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80);
				params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
				marca.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80);
				params.addRule(RelativeLayout.BELOW, R.id.marca);
				modelo.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80);
				params.addRule(RelativeLayout.BELOW, R.id.modelo);
				ano.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80);
				params.addRule(RelativeLayout.BELOW, R.id.ano);
				ubicacion.setLayoutParams(params);
				params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80);
				params.addRule(RelativeLayout.BELOW, R.id.ubicacion);
				precio.setLayoutParams(params);
			}


		}



		content.setOnKeyListener(new OnKeyListener()
		{
			/**
			 * This listens for the user to press the enter button on 
			 * the keyboard and then hides the virtual keyboard
			 */
			public boolean onKey(View arg0, int arg1, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ( (event.getAction() == KeyEvent.ACTION_DOWN  ) &&
						(arg1           == KeyEvent.KEYCODE_ENTER)   )
				{               
					InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(content.getWindowToken(), 0);   
					return true;
				}
				return false;
			}
		} );

		//Dar el valor de las preferencias a mis botones 
		Bundle extras=getIntent().getExtras();
		if(extras!=null){
			marcalista=extras.getString("marca");
			if(marcalista != null){
				editor.putString("marca", marcalista);

			}
			modelolista=extras.getString("modelo");
			if(modelolista != null){
				editor.putString("modelo", modelolista);
			}

			anolista=extras.getString("ano");
			if(anolista != null){
				editor.putString("ano", anolista);
			}

			ubicacionlista= extras.getString("ubicacion");
			if(ubicacionlista != null){
				editor.putString("ubicacion", ubicacionlista);
			}
			preciolista= extras.getString("precio");
			if(preciolista != null){
				editor.putString("precio", preciolista);
			}

			editor.commit();
		}

		//Valores por defecto de los botones
		marca.setText(preferences.getString("marca", "[Cualquiera]"));
		modelo.setText(preferences.getString("modelo", "[Cualquiera]"));
		ano.setText(preferences.getString("ano", "[Cualquiera]"));
		ubicacion.setText(preferences.getString("ubicacion", "[Cualquiera]"));
		precio.setText(preferences.getString("precio", "[Cualquiera]"));
		//		content.setText(preferences.getString("content", "Ejemplo : Jetta Rojo 2005"));

		//onClick de los botones

		//Ontouch del EditText
		//		content.setOnTouchListener(new View.OnTouchListener(){
		//			@Override
		//			public boolean onTouch(View arg0, MotionEvent arg1) {
		//				content.setText("");
		//				return false;
		//			}
		//		});

		marca.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				metodo = isOnline();
				if(metodo==false){
					AlertDialog.Builder builder = new AlertDialog.Builder(Buscar.this);
					builder.setMessage(R.string.necesita_conexi_n_a_internet)
					.setCancelable(false)
					.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();

				}else{
					String marcasb1 = metodomarca();
					Intent in = new Intent(Buscar.this, ListaMarca.class);
					in.putExtra("sb1", ""+marcasb1);

					//colocar desde cero las demas 
					SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = preferences.edit();
					editor.clear(); //marca modelo, ano ubicacion, precio
					editor.remove("modelo").remove("ano").remove("ubicacion").remove("precio");
					editor.commit();
					in.putExtra("content", ""+content.getText().toString().trim());
					startActivity(in);
				}
			}
		});

		modelo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				metodo = isOnline();
				if(metodo==false){
					AlertDialog.Builder builder = new AlertDialog.Builder(Buscar.this);
					builder.setMessage(R.string.necesita_conexi_n_a_internet)
					.setCancelable(false)
					.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();

				}else{
					if(marca.getText().toString().equals("[Cualquiera]")){
						final Dialog dialog = new Dialog(Buscar.this);
						dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
						dialog.setContentView(R.layout.rowcustom);
						Button btn = (Button) dialog.findViewById(R.id.cancel);
						btn.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								dialog.cancel();
							}
						});
						dialog.show();
					}else{
						String modelosb2 = metodomodelo();
						Intent in = new Intent(Buscar.this, ListaModelo.class);
						in.putExtra("sb2", ""+modelosb2);
						in.putExtra("content", ""+content.getText().toString().trim());
						startActivity(in);
					}
				}
			}
		});

		ano.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				metodo = isOnline();
				if(metodo == false){
					AlertDialog.Builder builder = new AlertDialog.Builder(Buscar.this);
					builder.setMessage(R.string.necesita_conexi_n_a_internet)
					.setCancelable(false)
					.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();

				}else{
					String anosb3 = metodoano();
					Intent in = new Intent(Buscar.this, ListaAno.class);
					in.putExtra("sb3", ""+anosb3);
					in.putExtra("content", ""+content.getText().toString().trim());
					startActivity(in);
				}
			}
		});

		http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?isnavigator=yes&navigatorname=year&query=


			ubicacion.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v){
					metodo = isOnline();
					if(metodo==false){
						AlertDialog.Builder builder = new AlertDialog.Builder(Buscar.this);
						builder.setMessage(R.string.necesita_conexi_n_a_internet)
						.setCancelable(false)
						.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							}
						});
						AlertDialog alertDialog = builder.create();
						alertDialog.show();

					}else{
						String ubicacionsb4 = metodoubica();
						Intent in=new Intent(Buscar.this, ListaUbicacion.class);
						in.putExtra("sb4", ""+ubicacionsb4);
						in.putExtra("content", ""+content.getText().toString().trim());
						startActivity(in);
					}
				}
			});

		precio.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				metodo = isOnline();
				if(metodo==false){
					AlertDialog.Builder builder = new AlertDialog.Builder(Buscar.this);
					builder.setMessage(R.string.necesita_conexi_n_a_internet)
					.setCancelable(false)
					.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();

				}else{
					String preciosb5 = metodoprecio();
					Intent in = new Intent(Buscar.this, ListaPrecio.class);
					in.putExtra("sb5", ""+preciosb5);
					in.putExtra("content", ""+content.getText().toString().trim());
					startActivity(in);
				}
			}
		});

		//		//Consulta para el Spinner
		//		Spinner spi = (Spinner) findViewById(R.id.spinner1);
		//		ArrayAdapter spinner_adapter = ArrayAdapter.createFromResource( this, R.array.buscar , android.R.layout.simple_spinner_item);
		//		spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//		spi.setAdapter(spinner_adapter);
		//		try {
		//			enviarInfo();
		//			
		//		} catch (Exception e) {
		//			
		//		}

		//onClick Buttones de Busqueda y de Limpiar
		buscar = (Button) findViewById(R.id.buscar);
		limpiar = (Button) findViewById(R.id.limpiar);




		limpiar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = preferences.edit();
				editor.clear(); //marca modelo, ano ubicacion, precio
				editor.remove("marca").remove("modelo").remove("ano").remove("ubicacion").remove("precio").remove("content");
				editor.commit();
				Log.d("Buscar", preferences.getString("marca", "si lo borro"));
				marca.setText("[Cualquiera]");
				modelo.setText("[Cualquiera]");
				ano.setText("[Cualquiera]");
				ubicacion.setText("[Cualquiera]");
				precio.setText("[Cualquiera]");
				content.setText("Ejemplo : Jetta Rojo 2005");
			}
		});

		buscar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//				Intent splash = new Intent(Buscar.this, Splash.class);
				//				startActivity(splash);
				Log.i("Inicio", "Inicio");


				metodo = isOnline();
				if(false){
					AlertDialog.Builder builder = new AlertDialog.Builder(Buscar.this);
					builder.setMessage(R.string.necesita_conexi_n_a_internet)
					.setCancelable(false)
					.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();

				}else{
					if(content.getText().toString().equals("Ejemplo : Jetta Rojo 2005")){
						String anosb9 =metodobusqueda();

						if(var2 == 0){
							final Dialog dialog = new Dialog(Buscar.this);
							dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
							dialog.setContentView(R.layout.rowcustom2);
							Button btn = (Button) dialog.findViewById(R.id.cancel);
							btn.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View v) {
									dialog.cancel();
								}
							});
							dialog.show();
						}else if(var2 == 1){
							SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
							SharedPreferences.Editor editor = preferences.edit();

							if(preferences.getBoolean("firsttime", true)){
								//							progress.setVisibility(View.GONE);
								//							Intent in = new Intent(Buscar.this, SplashPublicidad.class);
								Intent in = new Intent(Buscar.this, Resultados.class);

								in.putExtra("content", ""+content.getText().toString().trim());
								editor.putString("iscontent", "false");
								editor.commit();
								Log.i("ESTA ES LA BUENA", ""+anosb9);
								in.putExtra("sb9", ""+anosb9);
								startActivity(in);

							}else{
								Intent in = new Intent(Buscar.this, Resultados.class);
								in.putExtra("content", ""+content.getText().toString().trim());
								editor.putString("iscontent", "false");
								editor.commit();
								Log.i("ESTA ES LA BUENA", ""+anosb9);
								in.putExtra("sb9", ""+anosb9);
								startActivity(in);
							}

						}


					}else{
						String anosb9 =metodobusquedasincontent();
						if(var2 == 0){
							final Dialog dialog = new Dialog(Buscar.this);
							dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
							dialog.setContentView(R.layout.rowcustom2);
							Button btn = (Button) dialog.findViewById(R.id.cancel);
							btn.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View v) {
									dialog.cancel();
								}
							});
							dialog.show();
						}else if(var2 == 1){
							SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
							SharedPreferences.Editor editor = preferences.edit();

							if(preferences.getBoolean("firsttime", true)){
								//						Intent in = new Intent(Buscar.this, SplashPublicidad.class);
								Intent in = new Intent(Buscar.this, Resultados.class);

								in.putExtra("content", ""+content.getText().toString().trim());
								in.putExtra("issearch", "true");
								editor.putString("iscontent", "true");
								editor.putBoolean("fisttime", false);
								editor.commit();
								in.putExtra("sb9", ""+anosb9);
								startActivity(in);
							}else{
								Intent in = new Intent(Buscar.this, Resultados.class);
								in.putExtra("content", ""+content.getText().toString().trim());
								in.putExtra("issearch", "true");
								editor.putString("iscontent", "true");
								editor.commit();
								in.putExtra("sb9", ""+anosb9);
								startActivity(in);
							}


						}
					}
				}
			}
		});
	}
	//
	//	//Menú de opciones para las demás pantallas 
	//	public boolean onCreateOptionsMenu(Menu menu) {
	//	    super.onCreateOptionsMenu(menu);
	//	    MenuInflater inflater = getMenuInflater();
	//	    inflater.inflate(R.menu.menuprin, menu);
	//	    return true;
	//	}
	//	
	//	//Selección de cada una de las opciones del menú
	//	@Override
	//	public boolean onOptionsItemSelected(MenuItem item) {
	//		switch(item.getItemId()){
	//
	//		case R.id.migarage:
	//			Intent migarage  = new Intent(Buscar.this, MiGarage.class);
	//			startActivity(migarage);
	//			break;
	//			
	////		case R.id.noticias:
	////			Intent noticias  = new Intent(Buscar.this, Noticias.class);
	////			startActivity(noticias);
	////			break;
	////		
	////		case R.id.mas:
	////			Intent mas  = new Intent(Buscar.this, Mas.class);
	////			startActivity(mas);
	////			break;
	////			
	////		case R.id.guiaprecios:
	////			Intent guia  = new Intent(Buscar.this, GuiaPrecios.class);
	////			startActivity(guia);
	////			break;
	//		}		
	//		return super.onOptionsItemSelected(item);
	//	}

	//Metodo para el Spinner//
	protected void enviarInfo() {
		String url="http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?Homepage=yes";
		Log.i("url mia", ""+url);

		URI myURI = null;
		try {
			myURI = new URI(url);
		} catch (URISyntaxException e) {
			//				Toast.makeText(getApplicationContext(), "URISyntaxException", Toast.LENGTH_SHORT).show();
		}
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(myURI);
		HttpResponse webServerResponse = null;
		try {
			webServerResponse = httpClient.execute(getMethod);
		}  catch (IOException e) {
			//				Toast.makeText(getApplicationContext(), "IOException", Toast.LENGTH_SHORT).show();
		}

		HttpEntity httpEntity = webServerResponse.getEntity();

		//MANEJO DE LA RESPUESTA DEL SERVIDOR

		if (httpEntity != null) {

			try {
				is = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
				StringBuilder sb = new StringBuilder();
				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				is.close();
				result=sb.toString();
				String array[]=result.split("@@@");
				String array2[] = array[1].split("%%%");

				for(int i=0; i<array.length; i++){
					Log.i("MI ARRAY1", ""+array[i]);
				}
				for(int i=0; i<array2.length; i++){
					Log.i("MI ARRAY2", ""+array[i]);
				}

				//		            TextView nume= (TextView) findViewById(R.id.numeros);
				//		            nume.setText(""+array2[0]);

			} catch (IllegalStateException e) {
				//					Toast.makeText(getApplicationContext(), "IllegalStateException 2", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			} catch (IOException e) {
				//					Toast.makeText(getApplicationContext(), "IOException 2 ", Toast.LENGTH_SHORT).show();
			}
		}
	}

	//Metodos de seleccion para cada opción//
	//Metodo para la marca
	public String metodomarca(){
		var=0;
		sb1= new StringBuilder();
		marcaconcate ="http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?isnavigator=yes&navigatorname=marca&query=";
		StringBuilder sb = new StringBuilder();
		sb1.append(marcaconcate);
		Log.i("URL", sb1.toString());
		//			Toast.makeText(getApplicationContext(), ""+sb1.toString(), Toast.LENGTH_LONG).show();
		return sb1.toString();

	}

	//Para la modelo
	public String metodomodelo(){
		sb2= new StringBuilder();
		var=0;
		modeloconcate ="http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?isnavigator=yes&navigatorname=modelo&query=";
		StringBuilder sbpor = new StringBuilder();
		sb2.append(modeloconcate);
		if(!marca.getText().toString().equals("[Cualquier marca: ]")){
			sb2.append("marca;");
			try {
				textoEnviar2 = URLEncoder.encode(marca.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sbpor.append(array6[i]);
			}
			cadena2= sbpor.toString();

			sb2.append(cadena2);	
			var=1;
		}
		if(!ano.getText().toString().equals("[Cualquiera]")){
			StringBuilder anoepor = new StringBuilder();
			if(var==1){
				sb2.append(":");
			}
			sb2.append("year;");
			try {
				textoEnviar2 = URLEncoder.encode(ano.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				anoepor.append(array6[i]);
			}
			cadena2= anoepor.toString();

			sb2.append(cadena2);	
			var=2;
		}
		if(!ubicacion.getText().toString().equals("[Cualquiera]")){
			StringBuilder ubipor = new StringBuilder();
			if(var==1){

				sb2.append(":");
			}
			if(var==2){

				sb2.append(":");
			}
			sb2.append("estado;");
			try {
				textoEnviar2 = URLEncoder.encode(ubicacion.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				ubipor.append(array6[i]);
			}
			cadena2= ubipor.toString();

			sb2.append(cadena2);	
			var=3;
		}
		if(!precio.getText().toString().equals("[Cualquiera]")){
			StringBuilder prepor = new StringBuilder();
			if(var==1){

				sb2.append(":");
			}
			if(var==2){

				sb2.append(":");
			}else
				if(var==3){

					sb2.append(":");
				}
			sb2.append("rankprecio;");
			try {
				textoEnviar2 = URLEncoder.encode(precio.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				prepor.append(array6[i]);
			}
			cadena2= prepor.toString();
			sb2.append(cadena2);
			var=4;
		}
		Log.i("URL", sb2.toString());
		return sb2.toString();
	}

	//Metodo para la año
	public String metodoano(){
		sb3= new StringBuilder();
		var=0;
		anoconcate ="http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?isnavigator=yes&navigatorname=year&query=";
		StringBuilder sbano = new StringBuilder();
		sb3.append(anoconcate);
		if(!marca.getText().toString().equals("[Cualquiera]")){
			sb3.append("marca;");
			try {
				textoEnviar2 = URLEncoder.encode(marca.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sbano.append(array6[i]);
			}
			cadena4= sbano.toString();

			sb3.append(cadena4);	
			var=1;
		}
		if(!modelo.getText().toString().equals("[Cualquiera]")){
			StringBuilder modsb =new StringBuilder();
			if(var==1){

				sb3.append(":");
			}
			sb3.append("modelo;");
			try {
				textoEnviar3 = URLEncoder.encode(modelo.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array7= textoEnviar3.toCharArray();
			for(int i=0; i<array7.length;i++){
				if(array7[i]=='+'){
					array7[i]='_';
				}
			}
			for(int i=0;i<array7.length;i++){
				modsb.append(array7[i]);
			}
			cadena3= modsb.toString();

			sb3.append(cadena3);	
			var=2;
		}
		if(!ubicacion.getText().toString().equals("[Cualquiera]")){
			StringBuilder ubucasb =new StringBuilder();
			if(var==1){

				sb3.append(":");
			}
			if(var==2){

				sb3.append(":");
			}
			sb3.append("estado;");
			try {
				textoEnviar2 = URLEncoder.encode(ubicacion.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				ubucasb.append(array6[i]);
			}
			cadena2= ubucasb.toString();

			sb3.append(cadena2);	
			var=3;
		}
		if(!precio.getText().toString().equals("[Cualquiera]")){
			StringBuilder preciosb =new StringBuilder();
			if(var==1){
				sb3.append(":");
			}
			if(var==2){

				sb3.append(":");
			}
			if(var==3){
				sb3.append(":");
			}
			sb3.append("rankprecio;");
			try {
				textoEnviar2 = URLEncoder.encode(precio.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				preciosb.append(array6[i]);
			}
			cadena2= preciosb.toString();
			sb3.append(cadena2);	
			var=4;
		}
		Log.i("URL", sb3.toString());
		return sb3.toString();
	}

	//Metodo ubicacion
	public String metodoubica(){
		var=0;
		sb4= new StringBuilder();
		ubicaconcate ="http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?isnavigator=yes&navigatorname=estado&query=";
		StringBuilder sb = new StringBuilder();
		sb4.append(ubicaconcate);
		if(!modelo.getText().toString().equals("[Cualquiera]")){
			sb4.append("modelo;");
			try {
				textoEnviar2 = URLEncoder.encode(modelo.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sb.append(array6[i]);
			}
			cadena2= sb.toString();

			sb4.append(cadena2);	
			var=1;
		}
		if(!ano.getText().toString().equals("[Cualquiera]")){
			StringBuilder sbano = new StringBuilder();
			if(var==1){
				sb4.append(":");
			}
			sb4.append("year;");
			try {
				textoEnviar2 = URLEncoder.encode(ano.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sbano.append(array6[i]);
			}
			cadena2= sbano.toString();
			sb4.append(cadena2);	
			var=2;
		}
		if(!marca.getText().toString().equals("[Cualquiera]")){
			StringBuilder sbmarca = new StringBuilder();
			if(var==1){
				sb4.append(":");
			}
			if(var==2){
				sb4.append(":");
			}
			sb4.append("marca;");
			try {
				textoEnviar2 = URLEncoder.encode(marca.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sbmarca.append(array6[i]);
			}
			cadena2= sbmarca.toString();
			sb4.append(cadena2);	
			var=3;
		}
		if(!precio.getText().toString().equals("[Cualquiera]")){
			StringBuilder sbprecio = new StringBuilder();
			if(var==1){
				sb4.append(":");
			}
			if(var==2){
				sb4.append(":");
			}
			if(var==3){
				sb4.append(":");
			}
			sb4.append("rankprecio;");
			try {
				textoEnviar2 = URLEncoder.encode(precio.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sbprecio.append(array6[i]);
			}
			cadena2= sbprecio.toString();
			sb4.append(cadena2);	
			var=4;
		}
		Log.i("URL", sb4.toString());
		return sb4.toString();
	}

	//Metodo precio
	public String metodoprecio(){
		var=0;
		sb5= new StringBuilder();
		ubicaconcate ="http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?isnavigator=yes&navigatorname=rankprecio&query=";
		StringBuilder sb = new StringBuilder();
		sb5.append(ubicaconcate);
		if(!modelo.getText().toString().equals("[Cualquiera]")){
			sb5.append("modelo;");
			try {
				textoEnviar2 = URLEncoder.encode(modelo.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sb.append(array6[i]);
			}
			cadena2= sb.toString();
			sb5.append(cadena2);	
			var=1;
		}
		if(!ano.getText().toString().equals("[Cualquiera]")){
			StringBuilder sanob = new StringBuilder();
			if(var==1){
				sb5.append(":");
			}
			sb5.append("year;");
			try {
				textoEnviar2 = URLEncoder.encode(ano.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				sanob.append(array6[i]);
			}
			cadena2= sanob.toString();
			sb5.append(cadena2);	
			var=2;
		}
		if(!ubicacion.getText().toString().equals("[Cualquiera]")){
			StringBuilder subicacionb = new StringBuilder();
			if(var==1){
				sb5.append(":");
			}
			if(var==2){
				sb5.append(":");
			}
			sb5.append("estado;");
			try {
				textoEnviar2 = URLEncoder.encode(ubicacion.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				subicacionb.append(array6[i]);
			}
			cadena2= subicacionb.toString();

			sb5.append(cadena2);	
			var=3;
		}
		if(!marca.getText().toString().equals("[Cualquiera]")){
			StringBuilder smarcab = new StringBuilder();
			if(var==1){
				sb5.append(":");
			}
			if(var==2){
				sb5.append(":");
			}
			if(var==3){
				sb5.append(":");
			}
			sb5.append("marca;");
			try {
				textoEnviar2 = URLEncoder.encode(marca.getText().toString(), "utf-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			char[] array6= textoEnviar2.toCharArray();
			for(int i=0; i<array6.length;i++){
				if(array6[i]=='+'){
					array6[i]='_';
				}
			}
			for(int i=0;i<array6.length;i++){
				smarcab.append(array6[i]);
			}
			cadena2= smarcab.toString();

			sb5.append(cadena2);
			var=4;
		}
		Log.i("URL", sb5.toString());
		return sb5.toString();
	}

	//Metodo busqueda con palabra clave
	public String metodobusquedasincontent(){
		if((marca.getText().toString().equals("[Cualquiera]"))&&(modelo.getText().toString().equals("[Cualquiera]"))&&(ano.getText().toString().equals("[Cualquiera]"))&&(ubicacion.getText().toString().equals("[Cualquiera]"))&&(precio.getText().toString().equals("[Cualquiera]"))&&(content.getText().toString().equals("Ejemplo : Jetta Rojo 2005"))){
			var2 = 0;
			Log.i("No hay nada", ""+var2);
		}else{
			var2 = 1;
			var=0;
			StringBuilder sb1= new StringBuilder();
			marcaconcate = "http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?querybiphone=yes&BusqMult=";
			StringBuilder sb = new StringBuilder();
			sb1.append(marcaconcate);
			//				Toast.makeText(getApplicationContext(), "METODO CON CLAVE", Toast.LENGTH_LONG).show();
			if(!modelo.getText().toString().equals("[Cualquiera]")){
				sb1.append("modelo;");
				try {
					textoEnviar2 = URLEncoder.encode(modelo.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					sb.append(array6[i]);
				}
				cadena2= sb.toString();
				sb1.append(cadena2);
				var=1;
			}
			if(!ano.getText().toString().equals("[Cualquiera]")){
				StringBuilder sbpor1 = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				sb1.append("year;");
				try {
					textoEnviar2 = URLEncoder.encode(ano.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					sbpor1.append(array6[i]);
				}
				cadena2= sbpor1.toString();
				sb1.append(cadena2);
				var=2;
			}
			if(!ubicacion.getText().toString().equals("[Cualquiera]")){
				StringBuilder ubiporer = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				if(var==2){
					sb1.append(":");
				}
				sb1.append("estado;");
				try {
					textoEnviar2 = URLEncoder.encode(ubicacion.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					ubiporer.append(array6[i]);
				}
				cadena2= ubiporer.toString();
				sb1.append(cadena2);
				var=3;
			}
			if(!precio.getText().toString().equals("[Cualquiera]")){
				StringBuilder preciopor = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				if(var==2){
					sb1.append(":");
				}
				if(var==3){
					sb1.append(":");
				}
				sb1.append("rankprecio;");
				try {
					textoEnviar2 = URLEncoder.encode(precio.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					preciopor.append(array6[i]);
				}
				cadena2= preciopor.toString();
				sb1.append(cadena2);	
				var=4;
			}

			//para la marca
			if(!marca.getText().toString().equals("[Cualquiera]")){
				StringBuilder marcadime = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				if(var==2){
					sb1.append(":");
				}
				if(var==3){
					sb1.append(":");
				}
				if(var==4){
					sb1.append(":");
				}
				sb1.append("marca;");
				try {
					textoEnviar2 = URLEncoder.encode(marca.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					marcadime.append(array6[i]);
				}
				cadena2= marcadime.toString();
				sb1.append(cadena2);	
				var=5;
			}

			//para el contenido
			if(!content.getText().toString().equals("Ejemplo:Jetta Rojo 2012")){
				StringBuilder dcontent = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				if(var==2){
					sb1.append(":");
				}
				if(var==3){
					sb1.append(":");
				}
				if(var==4){
					sb1.append(":");
				}
				if(var==5){
					sb1.append(":");
				}
				sb1.append("content;");
				try {
					textoEnviar2 = URLEncoder.encode(content.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					dcontent.append(array6[i]);
				}
				cadena2= dcontent.toString();
				sb1.append(cadena2);
				cadenabase = "&titlebiphone=yes&offset=0&hits=10&VIP=yes";
				sb1.append(cadenabase);
				Log.i("URL", sb1.toString());
				return sb1.toString();
			}else{
				return null;
			}
		}
		return null;
	}

	//Metodo para busqueda sin palabra clave
	//**CHECAR ESTO
	public String metodobusqueda(){
		if((marca.getText().toString().equals("[Cualquiera]"))&&(modelo.getText().toString().equals("[Cualquiera]"))&&(ano.getText().toString().equals("[Cualquiera]"))&&(ubicacion.getText().toString().equals("[Cualquiera]"))&&(precio.getText().toString().equals("[Cualquiera]"))&&(content.getText().toString().equals("Ejemplo : Jetta Rojo 2005"))){
			var2 = 0;
			Log.i("IF", ""+var2);
		}else{
			Log.i("ELSE", ""+var2);

			var2 = 1;
			var=0;
			StringBuilder sb1= new StringBuilder();
			marcaconcate = "http://iphone.autoplaza.com.mx/fastSearch/fastSearchIphone.aspx?querybiphone=yes&BusqMult=";
			StringBuilder sb = new StringBuilder();
			sb1.append(marcaconcate);
			//				Toast.makeText(getApplicationContext(), "METODO SIN CLAVE", Toast.LENGTH_LONG).show();
			if(!modelo.getText().toString().equals("[Cualquiera]")){
				sb1.append("modelo;");
				try {
					textoEnviar2 = URLEncoder.encode(modelo.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					sb.append(array6[i]);
				}
				cadena2= sb.toString();
				sb1.append(cadena2);
				var=1;
			}
			if(!ano.getText().toString().equals("[Cualquiera]")){
				StringBuilder sbpor1 = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				sb1.append("year;");
				try {
					textoEnviar2 = URLEncoder.encode(ano.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					sbpor1.append(array6[i]);
				}
				cadena2= sbpor1.toString();
				sb1.append(cadena2);
				var=2;
			}
			if(!ubicacion.getText().toString().equals("[Cualquiera]")){
				StringBuilder ubiporer = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				if(var==2){
					sb1.append(":");
				}
				sb1.append("estado;");
				try {
					textoEnviar2 = URLEncoder.encode(ubicacion.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					ubiporer.append(array6[i]);
				}
				cadena2= ubiporer.toString();
				sb1.append(cadena2);
				var=3;
			}
			if(!precio.getText().toString().equals("[Cualquiera]")){
				StringBuilder preciopor = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				if(var==2){
					sb1.append(":");
				}
				if(var==3){
					sb1.append(":");
				}
				sb1.append("rankprecio;");
				try {
					textoEnviar2 = URLEncoder.encode(precio.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					preciopor.append(array6[i]);
				}
				cadena2= preciopor.toString();
				sb1.append(cadena2);	
				var=4;
			}

			//para la marca
			if(!marca.getText().toString().equals("[Cualquiera]")){
				StringBuilder marcadime = new StringBuilder();
				if(var==1){
					sb1.append(":");
				}
				if(var==2){
					sb1.append(":");
				}
				if(var==3){
					sb1.append(":");
				}
				if(var==4){
					sb1.append(":");
				}
				sb1.append("marca;");
				try {
					textoEnviar2 = URLEncoder.encode(marca.getText().toString(), "utf-8");
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				char[] array6= textoEnviar2.toCharArray();
				for(int i=0; i<array6.length;i++){
					if(array6[i]=='+'){
						array6[i]='_';
					}
				}
				for(int i=0;i<array6.length;i++){
					marcadime.append(array6[i]);
				}
				cadena2= marcadime.toString();
				sb1.append(cadena2);	
				//				cadenabase = "&titlebiphone=yes&offset=0&hits=10&VIP=yes";
				//				sb1.append(cadenabase);
				Log.i("URL", sb1.toString());
			}
			cadenabase = "&titlebiphone=yes&offset=0&hits=10&VIP=yes";

			sb1.append(cadenabase);
			return sb1.toString();

		}
		return null;
	}

	/*METODO PARA CONTROLAR EL onFinish()*/
	@Override
	public void finish() {
		super.finish();
		SharedPreferences preferences= getSharedPreferences("Carro",Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.clear(); //marca modelo, ano ubicacion, precio
		editor.remove("marca").remove("modelo").remove("ano").remove("ubicacion").remove("precio").remove("content");
		editor.commit();
		Log.d("Buscar", "Entré a Finish");
	}

	//CHECA LA CONEXION A INTERNET
	public boolean isOnline() {
		NetworkInfo networkInfo = ((ConnectivityManager) getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE))
				.getActiveNetworkInfo();

		if(networkInfo == null || !networkInfo.isConnected()) {
			return false;
		}
		return true;
	}

	//        public static boolean isInternetConnectionActive(Context context) {
	//            NetworkInfo networkInfo = ((ConnectivityManager) context
	//              .getSystemService(Context.CONNECTIVITY_SERVICE))
	//              .getActiveNetworkInfo();
	//
	//            if(networkInfo == null || !networkInfo.isConnected()) {
	//             return false;
	//            }
	//            return true;
	//  }
}
